package com.casems.controller;


import com.casems.commons.dto.CasesDTO;
import com.casems.commons.util.JwtUtil;
import com.casems.commons.vo.CasesVO;
import com.casems.commons.vo.ResultVO;
import com.casems.entity.User;
import com.casems.service.CasesService;
import com.casems.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tch
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/cases")
public class CasesController {

    @Autowired
    CasesService casesService;

    @Autowired
    UserService userService;

    @RequestMapping("/getCaseAnalyseData")
    public ResultVO getCaseAnalyseData(){
        Map<String,Object> data = casesService.getCaseAnalyseData();
        return data!=null? ResultVO.success(data):ResultVO.failed("查询失败");
    }

    @RequestMapping("/getCaseData")
    public ResultVO getCaseData(){
        Map<String,Object> data = casesService.getCaseData();
        return data!=null? ResultVO.success(data):ResultVO.failed("查询失败");
    }

    @RequestMapping("/getCaseDailyData")
    public ResultVO getCaseDailyData(){
        Map<String,Integer> data = casesService.getCaseDailyData();
        return data!=null? ResultVO.success(data):ResultVO.failed("查询失败");
    }

    @RequestMapping("/getCasesByToken")
    public ResultVO getByToken(@RequestHeader("Authorization") String token){
        String phone = JwtUtil.getPhone(token);
        User user = userService.getUser(phone);
        List<CasesDTO> cases = casesService.getByUserId(user.getId());
        return cases!=null? ResultVO.success(cases):ResultVO.failed("查询失败");
    }

    @RequestMapping("/getCasesByUser")
    public ResultVO getCasesByUser(@RequestBody User user){
        Map<String,Object> data = new HashMap<String,Object>();

        data.put("user",userService.getUserForCase(user));
        List<CasesDTO> cases = casesService.getByUser(user);
        data.put("cases", cases);
        return data.get("user")!=null? ResultVO.success(data):ResultVO.failed("用户信息不存在");
    }

    @PutMapping("/addCases")
    public ResultVO addCases(@RequestBody CasesVO casesVO){
        return casesService.addCases(casesVO)?
                ResultVO.success("添加成功"):ResultVO.failed("添加失败");
    }
    @DeleteMapping("/deleteCases")
    public ResultVO deleteCases(@RequestBody CasesVO casesVO){
        return casesService.deleteCases(casesVO)?
                ResultVO.success("删除成功"):ResultVO.failed("删除失败");
    }

    @PostMapping("/updateCase")
    public ResultVO updateCase(@RequestBody CasesVO casesVO){
        return casesService.updateCase(casesVO)?
                ResultVO.success("修改成功"):ResultVO.failed("修改失败");
    }
}

